#include <windows.h>
#include <string.h>
#include <cwchar>
#include <iostream>
using namespace std;
#define DllForInjecting L"DllForInjecting.dll"
#define TargetApp L"Target App.exe"
void main()
{
    STARTUPINFO si;
    PROCESS_INFORMATION pi;

    ZeroMemory( &si, sizeof(si) );
    si.cb = sizeof(si);
    ZeroMemory( &pi, sizeof(pi) );

	wchar_t *TAppName=new wchar_t[wcslen(TargetApp)+1];
	 TAppName[0]=0;wcscpy(TAppName,TargetApp);

	   if( !CreateProcess( NULL,   // No module name (use command line)
			TAppName,        // Command line
			NULL,           // Process handle not inheritable
			NULL,           // Thread handle not inheritable
			FALSE,          // Set handle inheritance to FALSE
			CREATE_SUSPENDED,              // No creation flags
			NULL,           // Use parent's environment block
			NULL,           // Use parent's starting directory 
			&si,            // Pointer to STARTUPINFO structure
			&pi )           // Pointer to PROCESS_INFORMATION structure
		 ) 
        cout<< "CreateProcess failed GetLastError "<<GetLastError()<<endl;
	 HANDLE RhThread;
	 int size;
	 wchar_t *str=new wchar_t[size=(wcslen(DllForInjecting)+1)];
	 str[0]=0;wcscpy(str,DllForInjecting);

	 PTHREAD_START_ROUTINE pfnThreadRtn = (PTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandle(TEXT("Kernel32")), "LoadLibraryW");
	 void* Pstr=VirtualAllocEx(pi.hProcess,NULL,size*sizeof(wchar_t),MEM_COMMIT|MEM_RESERVE,PAGE_READWRITE);
	 if(!Pstr) cout<< "Error Allocating memory in remote process GetLastError "<<GetLastError()<<endl;
	if(!WriteProcessMemory(pi.hProcess,Pstr,(void*)str,size*sizeof(wchar_t),NULL)) cout<< "Error writing memory in remote process GetLastError "<<GetLastError()<<endl;

	 if(!(RhThread= CreateRemoteThread(pi.hProcess, NULL, 0,pfnThreadRtn, Pstr, 0, NULL)))
		cout<< "CreateRemoteThread failed GetLastError "<<GetLastError()<<endl;	
	 else
	 {
		 WaitForSingleObject(RhThread,INFINITE);
		 ResumeThread(pi.hThread);
	 }
	 delete [] str;
	 delete [] TAppName;
}

